/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tr3dtris.game;

import javax.swing.event.EventListenerList;
import tr3dtris.game.events.GameOverEvent;
import tr3dtris.game.listeners.GameActionsListener;

/**
 *
 * @author Luciano
 */
public class GameEvents
{

    //listeners
    protected EventListenerList gameActionsListenerList;

    public GameEvents()
    {
        this.gameActionsListenerList = new EventListenerList();
    }
    
    public void addGameActionsListener(GameActionsListener gameActionsListener)
    {
        this.gameActionsListenerList.add(GameActionsListener.class, gameActionsListener);
    }

    public void removeGameActionsListener(GameActionsListener gameActionsListener)
    {
        this.gameActionsListenerList.remove(GameActionsListener.class, gameActionsListener);
    }


    public void fireGameOver(Object source, int points, int linesErased)
    {
        GameOverEvent evt = new GameOverEvent(source, points, linesErased);
        Object[] listeners = this.gameActionsListenerList.getListenerList();
            // Each listener occupies two elements - the first is the listener class
            // and the second is the listener instance
            for (int i=0; i<listeners.length; i+=2) {
                if (listeners[i]== GameActionsListener.class) {
                    ((GameActionsListener)listeners[i+1]).gameOverOccured(evt);
                }
            }
    }

}
